<?php
include "../model/db.php"; //引入数据库
include '../model/fn.php'; //公共函数
//删除操作
if($_GET['type']=='sc'&&$_GET['id']){
  $sql="delete from bl_wz where id = '{$_GET['id']}' limit 1";
  if(db_del($sql)){
    echo "<script>alert('操作成功');</script>";
  }else{
    echo "<script>alert('操作失败');</script>";
  }
}

$fy=''; //分页携带的参数
//实现搜索
$where='where';
//判断是否搜索分类
if(!empty($_GET['cid'])){
  $where.=" w.cid='{$_GET['cid']}' and";
  $fy.='&cid='.$_GET['cid'];
}
//判断是否搜索标题；
if(!empty($_GET['title'])){
  $where.=" w.title like '%{$_GET['title']}%' and";
  $fy.='&title='.$_GET['title'];
}

if($where=='where'){
  //没有搜索条件
  $where='';
}else{
  //有搜索条件
  $where=trim($where,'and');
}

/*****排序******/
$order='order by';
if($_GET['ortime']=='1'){
  $order.=' w.time asc ,';
  $fy.='&ortime='.$_GET['ortime'];
}else if($_GET['ortime']=='2'){
  $order.=' w.time desc ,';
  $fy.='&ortime='.$_GET['ortime'];
}

if($_GET['orid']=='1'){
  $order.=' w.id asc ,';
  $fy.='&orid='.$_GET['orid'];
}elseif($_GET['orid']=='2'){
  $order.=' w.id desc ,';
  $fy.='&orid='.$_GET['orid'];
}

//判断是否有排序
if($order!='order by'){
  $order=trim($order,',');
}else{
  $order='';
}

//*****/分页#########/
$limit=2; //一页显示的条数
$page=$_GET['page']?$_GET['page']:1; //当前页数
$offset=($page-1)*$limit; //偏移量，就是limit的第1个参数

//不知道有多少页，确定不了页数
$cot=db_sel("select count(*) as co from bl_wz as w $where");
$count=ceil($cot['co']/$limit); //得到一共有多少页

//得到上一页和下一页
//上一页 = 当前页-1，不能小于1
if($page-1<1){
  $syy='1';
}else{
  $syy=$page-1;
}

//下一页=当前页+1，不能超过总页数
if($page+1>$count){
  $xyy=$count;
}else{
  $xyy=$page+1;
}




$sql="select w.*,c.name from bl_wz as w join bl_class as c on w.cid = c.id $where $order limit $offset,$limit";
$arr=db_sel($sql,2); //查询出文章

//得到所有的分类
$sql="select * from bl_class";
$class=db_sel($sql,2); //查询所有

//无限极排序---递归实现
$list=fl($class);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>后台 管理中心 - 文章列表 </title>
  <meta name="robots" >
  <meta http-equiv="Content-Type" />
  <link href="../public/admin/styles/general.css" rel="stylesheet" type="text/css" />
  <link href="../public/admin/styles/main.css" rel="stylesheet" type="text/css" />
</head>
<body>

<h1>
  <span class="action-span"><a href="goodsadd.php">添加新文章</a></span>
  <span class="action-span1"><a href="#">后台 管理中心</a> </span><span id="search_id" class="action-span1"> - 文章列表 </span>
  <div style="clear:both"></div>
</h1>

<div class="form-div">
  <!--  搜索功能-->
  <form action="goodslist.php" name="searchForm" method="get">
    <img src="../public/admin/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
    <select name="cid">
      <option value="0">全部</option>
      <?php foreach($list as $v){?>
        <option  <?php if($v['id']==$_GET['cid']){echo 'selected';}?> value="<?php echo $v['id']?>"><?php echo $v['name']?></option>
      <?php }?>
    </select>

    标题 <input type="text" name="title" value="<?php echo $_GET['title']?>" size="15" />

    <img src="../public/admin/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
    时间:
    升序 <input type="radio" <?php if($_GET['ortime']=='1'){echo 'checked';}?>  name="ortime" value="1">
    | 降序 <input type="radio" <?php if($_GET['ortime']=='2'){echo 'checked';}?> name="ortime" value="2">
    &nbsp;&nbsp;&nbsp;
    编号：
    升序<input type="radio" <?php if($_GET['orid']=='1'){echo 'checked';}?> name="orid" value="1">
    | 降序 <input type="radio" <?php if($_GET['orid']=='1'){echo 'checked';}?> name="orid" value="2">
    <input type="submit" value="提交" class="button" />
  </form>
</div>

<form method="post" action="" name="listForm" >

  <div class="list-div" id="listDiv">
    <!--    列表-->
    <table cellpadding="3" cellspacing="1">
      <tr>
        <th>
          <a href="#">编号</a><img src="../public/admin/images/sort_desc.gif"/>    </th>
        <th><a href="#">文章标题</a></th>
        <th><a href="#">作者</a></th>
        <th><a href="#">分类</a></th>
        <th><a href="#">阅读人数</a></th>
        <th><a href="#">是否显示</a></th>
        <th><a href="#">发布时间</a></th>
        <th>操作</th>
      </tr>

      <!--      遍历显示-->
      <?php foreach($arr as $v){?>
        <tr>
          <td>
            <?php echo $v['id']?>
          </td>
          <td><a href="#"><?php echo $v['title']?></a></td>
          <td><a href="#"> <?php echo $v['user']?></a></td>
          <td><a href="#"> <?php echo $v['name']?></a></td>
          <td><a href="#"> <?php echo $v['ydl']?></a></td>
          <td><a href="#"> <?php echo $v['stat']=='1'?'显示':'不显示';?></a></td>
          <td><a href="#"> <?php echo date('Y-m-d H:i:s',$v['time'])?></a></td>
          <td>
            <a href="./goodsupd.php?id=<?php echo $v['id']?>">修改</a>
            |
            <a href="./goodslist.php?type=sc&id=<?php echo $v['id']?>">删除</a>
            |
            <a href="./pllist.php?id=<?php echo $v['id']?>">评论</a>
          </td>
        </tr>
      <?php }?>

    </table>

    <table id="page-table" cellspacing="0">
      <tr>
        <td align="right" nowrap="true">

          <a href="./goodslist.php?page=<?php echo $syy.$fy?>">上一页</a>
          &nbsp;&nbsp;
          <!--      求页码-->
          <!--  当前页前面的页码    -->
          <?php $qi=$page-5>0?$page-5:'1'; for($i=$qi;$i<$page;$i++){?>
            <a href="./goodslist.php?page=<?php echo $i.$fy?>"><?php echo $i?></a>
          <?php }?>

          <!--  当前页码    -->
          <span style="color: red"><?php echo $page?></span>

          <!--  当前页后面的页码    -->
          <?php $hi=$page+6>$count?$count:$page+6; for($j=$page+1;$j<=$hi;$j++){?>
            <a href="./goodslist.php?page=<?php echo $j.$fy?>"><?php echo $j?></a>
          <?php }?>
          &nbsp;&nbsp;&nbsp;
          <a href="./goodslist.php?page=<?php echo $xyy.$fy?>">下一页</a>
        </td>
      </tr>
    </table>

  </div>
</form>

<div id="footer">
共执行 7 个查询，用时 0.112141 秒，Gzip 已禁用，内存占用 3.085 MB<br />
版权所有 &copy; 2005-2010 上海商派网络科技有限公司，并保留所有权利。</div>
</body>
</html>